package fy.mdm.controller;


import fy.mdm.dao.EnclosureService;
import fy.mdm.dao.FiexdAssetsDao;
import fy.mdm.dao.MaterialCodeDao;
import fy.mdm.dao.MesCCProductInfoFindDao;
import fy.mdm.dao.NewFinDao;
import fy.mdm.dao.PurchasePartsDao;
import fy.mdm.dao.SrmBCategoryDevelopDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;

public class OA2MDMController extends BaseController {
	private static final String findMaterialInfoJSP = "fy_view/oa2mdm/Material/findMaterialInfoByCode";
	private static final String writeMaterialInfoJSP = "fy_view/oa2mdm/Material/writeMaterialInfo";
	private static final String yanMaterialInfoJSP = "fy_view/oa2mdm/Material/yanMaterialInfoBy";
	private static final String findProductInfoJSP = "fy_view/oa2mdm/MesCC/findProductInfoByCode";
	private static final String writeProductInfoJSP = "fy_view/oa2mdm/MesCC/writeproductInfo";
	private static final String findSrmBCategoryJSP = "fy_view/oa2mdm/SrmBCategory/findMaterialInfoByCode";
	private static final String writeSrmBCategoryJSP = "fy_view/oa2mdm/SrmBCategory/writeMaterialInfo";
	private static final String findFiexdAssetsInfoJSP = "fy_view/oa2mdm/FiexdAssets/findFiexdAssetsByCode";
	private static final String writeFiexdAssetsInfoJSP = "fy_view/oa2mdm/FiexdAssets/writeFiexdAssetsInfo";

	private static final String newfindeJSP = "fy_view/oa2mdm/NewFinde/NewFinde";
	private static final String writenewfindeJSP = "fy_view/oa2mdm/NewFinde/writeNewFinde";
	
	/*private static final String newattachmentJSP = "fy_view/oa2mdm/Attachment/findAttachment";
	private static final String writeattachmentJSP = "fy_view/oa2mdm/Attachment/writeAttachment";*/
	
	private static final String findPurchaseParts = "fy_view/oa2mdm/PurchaseParts/findPurchaseParts";
	private static final String writePurchaseParts = "fy_view/oa2mdm/PurchaseParts/writePurchaseParts";

	public ModelAndView getMaterialInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findMaterialInfoJSP);
		}
		return modelAndView;
	}

	public ModelAndView getyanMaterialInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(yanMaterialInfoJSP);
		}
		return modelAndView;
	}

	public ModelAndView writeMaterialInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writeMaterialInfoJSP);

		String materialCode = request.getParameter("materialCode");
		String differentiateForm = request.getParameter("differentiateForm");
		String materialtype = request.getParameter("Materialtype");
		String operate = request.getParameter("operate");

		String newModel = request.getParameter("newModel");
		String newBrand = request.getParameter("newBrand");

		response.setCharacterEncoding("utf-8");
		MaterialCodeDao mcd = new MaterialCodeDao();
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			materialCode = materialCode.replaceAll(".*([';]+|(--)+).*", " ");
			String[] materialCodeArray = materialCode.split(",");
			if (operate.equals("1")) {
				String[] materialTypeArray = materialtype.split(",");
				mcd.setdifferentiateForm("true".equals(differentiateForm));
				mcd.setMaterialtype(materialTypeArray);
				modelAndView.addObject("materialCodeJSON",
						mcd.getMaterialInfo(materialCodeArray).toString());
			}
			if (operate.equals("2")) {
				mcd.setMaterialCode(materialCodeArray);
				if (!newModel.equals(null)) {
					String[] newModelArray = newModel.split(",");
					mcd.setNewModel(newModelArray);
				}
				if (!newBrand.equals(null)) {
					String[] newBrandArray = newBrand.split(",");
					mcd.setNewBrand(newBrandArray);
				}
				modelAndView.addObject("materialCodeJSON", mcd
						.materialCodeChange().toString());
			}
			success = true;
		}
		if (!success) {
			modelAndView.addObject("materialCodeJSON", "{}");
		}
		return modelAndView;
	}

	public ModelAndView getSrmBCategoryJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findSrmBCategoryJSP);
		}
		return modelAndView;
	}

//	public ModelAndView writeSrmBCategoryJSP(HttpServletRequest request,
//			HttpServletResponse response) throws Exception {
//		ModelAndView modelAndView = null;
//		modelAndView = new ModelAndView(writeSrmBCategoryJSP);
//		String materialCode = request.getParameter("materialCode");
//		response.setCharacterEncoding("utf-8");
//		SrmBCategoryDevelopDao sd = new SrmBCategoryDevelopDao();
//		boolean success = false;
//		if (AppContext.getCurrentUser() != null) {
//			materialCode = materialCode.replaceAll(".*([';]+|(--)+).*", " ");
//			String[] materialCodes = materialCode.split(",");
//			sd.setCodes(materialCodes);
//			modelAndView.addObject("materialCodeJSON", sd
//					.LookUpInfoBymaterialCode().toString());
//			success = true;
//		}
//		if (!success) {
//			modelAndView.addObject("materialCodeJSON", "{}");
//		}
//		return modelAndView;
//	}
	public ModelAndView writeSrmBCategoryJSP(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writeSrmBCategoryJSP);
		String materialCode = request.getParameter("materialCode");
		//String flowcode = request.getParameter("flowcode");
		//System.out.println(flowcode);
		//String differentiateForm = request.getParameter("differentiateForm");
		//System.out.println("~~~~~~~~~~~~~~~~~" + materialCode);
		response.setCharacterEncoding("utf-8");
	//	MaterialCodeDao mcd = new MaterialCodeDao();
		SrmBCategoryDevelopDao sd=new SrmBCategoryDevelopDao();
		boolean success = false;
	//	if (super.getCurrentUser() != null && (materialCode != null) && (!"".equals(materialCode))) {
			if (AppContext.getCurrentUser() != null ) {
			materialCode = materialCode.replaceAll(".*([';]+|(--)+).*", " ");
		//	if (!"".equals(materialCode)) {
		       String[] materialCodes = materialCode.split(",");
		//		if (materialCodes != null && materialCodes.length != 0) {
	//		mcd.setdifferentiateForm("true".equals(differentiateForm));
					sd.setCodes(materialCodes);
					//sd.setFlowcode(flowcode);
			
			modelAndView.addObject("materialCodeJSON", sd.LookUpInfoBymaterialCode().toString());
			success = true;
	//	}
				//}
				}
		if (!success) {
			modelAndView.addObject("materialCodeJSON", "{}");
		}

		return modelAndView;
	}

	public ModelAndView getFiexdAssetsInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findFiexdAssetsInfoJSP);
		}
		return modelAndView;
	}

	public ModelAndView writeFiexdAssetsInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writeFiexdAssetsInfoJSP);
		String fiexdCode = request.getParameter("fiexdCode");
		String lineCode = request.getParameter("lineCode");

		response.setCharacterEncoding("utf-8");
		FiexdAssetsDao fadao = new FiexdAssetsDao();
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			fiexdCode = fiexdCode.replaceAll(".*([';]+|(--)+).*", " ");
			lineCode = lineCode.replaceAll(".*([';]+|(--)+).*", " ");

			String[] fiexdCodeArray = fiexdCode.split(",");
			String[] lineCodeArray = lineCode.split(",");

			fadao.setCodes(fiexdCodeArray);
			fadao.setLines(lineCodeArray);

			modelAndView.addObject("fiexdCodeJSON", fadao.getFiexdAssetsInfo()
					.toString());
			success = true;
		}
		if (!success) {
			modelAndView.addObject("fiexdCodeJSON", "{}");
		}
		return modelAndView;
	}

	public ModelAndView getProductInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findProductInfoJSP);
		}
		return modelAndView;
	}

	public ModelAndView writeProductInfoJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writeProductInfoJSP);
		String productCode = request.getParameter("productCode");
		response.setCharacterEncoding("utf-8");
		MesCCProductInfoFindDao mcd = new MesCCProductInfoFindDao();
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			productCode = productCode.replaceAll(".*([';]+|(--)+).*", " ");
			//System.err.println("获取编号："+productCode);
			if (!"".equals(productCode)) {
				String[] productArray = productCode.split(",");
				if ((productArray != null) && (productArray.length != 0)) {
					mcd.setCodes(productArray);
					modelAndView.addObject("ProductInfoJSON",
							mcd.findProductInfo());

					success = true;
				}
			}
		}
		if (!success) {
			modelAndView.addObject("ProductInfoJSON", "{}");
		}
		return modelAndView;
	}

	// //---------------------销售
	public ModelAndView getNewFindeJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(newfindeJSP);
		}
		return modelAndView;
	}

	public ModelAndView writeNewFindeJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writenewfindeJSP);
		String planidInput = request.getParameter("planidInput");
		// String FactoryId = request.getParameter("FactoryId");
		response.setCharacterEncoding("utf-8");
		NewFinDao nfd = new NewFinDao();
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			planidInput = planidInput.replaceAll(".*([';]+|(--)+).*", " ");
			if (!"".equals(planidInput)) {
				nfd.setPlanid(planidInput);
				modelAndView.addObject("NewFindeJSP", nfd.newfindeInfo()
						.toString());
				success = true;
			}
		}
		// }
		if (!success) {
			modelAndView.addObject("NewFindeJSP", "{}");
		}
		return modelAndView;
	}
	 //---------------------附件
	/*public ModelAndView getAttachmentJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(newattachmentJSP);
		}
		return modelAndView;
	}

	public ModelAndView writeAttachmentJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writeattachmentJSP);
		String materialCode = request.getParameter("materialCode");
		// String FactoryId = request.getParameter("FactoryId");
		logger.info("materialCode:"+materialCode);
		response.setCharacterEncoding("utf-8");
		EnclosureService enc = new EnclosureService();
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			materialCode = materialCode.replaceAll(".*([';]+|(--)+).*", " ");
			// FactoryId = FactoryId.replaceAll(".*([';]+|(--)+).*", " ");
			if (!"".equals(materialCode)) {
				enc.setMain_data_id(materialCode);
				logger.info(enc.getMain_data_id());
				modelAndView.addObject("Attachment",enc.getEnclosureService().toString());
				success = true;
			}
		}
		// }
		if (!success) {
			modelAndView.addObject("Attachment", "{}");
		}
		return modelAndView;
	}*/
	//-----------------采购件新增
	public ModelAndView getPurchasePartsJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		if (AppContext.getCurrentUser() != null) {
			modelAndView = new ModelAndView(findPurchaseParts);
		}
		return modelAndView;
	}
	
	public ModelAndView writePurchasePartsJSP(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = null;
		modelAndView = new ModelAndView(writePurchaseParts);
		String ITEM = request.getParameter("ITEM");
		response.setCharacterEncoding("utf-8");
		PurchasePartsDao PurchasePartsdao=new PurchasePartsDao();
		boolean success = false;
		if (AppContext.getCurrentUser() != null) {
			ITEM = ITEM.replaceAll(".*([';]+|(--)+).*", " ");
			if (!"".equals(ITEM)) {
				String[] ITEMArray = ITEM.split(",");
				PurchasePartsdao.setITEM(ITEMArray);
				modelAndView.addObject("PurchaseCodeJSON", PurchasePartsdao.getPurchasePartsInfo()
						.toString());
				success = true;
			}
		}
		// }
		if (!success) {
			modelAndView.addObject("PurchaseCodeJSON", "{}");
		}
		return modelAndView;
	}
}

// }
